package com.shujia.day14;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapDemo2 {
    public static void main(String[] args) {
        //Map集合遍历
        HashMap<Integer, String> map1 = new HashMap<>();
        map1.put(1001, "李刚1");
        map1.put(1002, "李刚2");
        map1.put(1003, "李刚3");
        map1.put(1001, "李刚4");
        map1.put(1004, "李刚5");
        System.out.println("map1: " + map1);
        System.out.println("----------------------");

        // 方式1：获取所有的键，遍历键，根据键获取值
//        Set<Integer> keySet = map1.keySet();
//        for (Integer key : keySet) {
//            String value = map1.get(key);
//            System.out.println(key + "-" + value);
//        }

        // 方式2：直接获取所有的键值对，遍历每一个键值对得到每一个键和值
        //Set<Map.Entry<K,V>> entrySet()
        Set<Map.Entry<Integer, String>> entries = map1.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + ":" + value);
        }
    }
}
